package mylab.usm.oauth2.client.service;

import cn.hutool.core.bean.BeanUtil;
import com.fujieid.jap.core.JapUser;
import lombok.RequiredArgsConstructor;
import me.zhyd.oauth.model.AuthUser;
import mylab.usm.api.model.account.User;
import mylab.usm.api.service.UserService;
import mylab.usm.oauth2.client.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@Service
public class JapUserServiceImpl implements JapUserService {

    private final UserService userService;

    @Override
    public JapUser getById(String userId) {
        return Util.fromSysUser(userService.getById(userId));
    }

    @Override
    public JapUser getByUsername(String username) {
        return Util.fromSysUser(userService.getByUsername(username));
    }

    @Override
    public boolean validPassword(String password, JapUser user) {
        return userService.validPassword(user.getUserId(), password);
    }

    @Override
    public JapUser getByPlatformAndUid(String platformId, String uid) {
        return Util.fromSysUser(userService.getByPlatformAndUid(platformId, uid));
    }

    @Override
    public JapUser createUser(AuthUser userInfo) {
        User user = userService.createByPlatform(userInfo.getSource(), BeanUtil.beanToMap(userInfo));
        return Util.fromSysUser(user);
    }
}
